<template>
	<view class="container">
		<image class="top-bg" src="@/static/main_top_bg.png" mode="widthFix"></image>
		<uni-nav-bar leftText="客满" :leftWidth="300" statusBar :border="false"
			backgroundColor="transparent"></uni-nav-bar>
		<mainInput @click="handleSearch" />
		<banner v-if="sliders.length" :sliders="sliders" />
		<wCard v-if="project_cates.length" flexDirection="row" paddingBottom="23">
			<gridItem v-for="(item, index) in project_cates" :key="index" :itemData="item"
				@itemClick="handleMenuClick" />
		</wCard>
		<baseTitle v-if="news_cates.length" title="地市产业政策" @moreClick="handleArticleList" />

		<wCard v-if="news_cates.length" flexDirection="row" marginTop="18" paddingBottom="27" paddingLeft="13"
			paddingTop="18">
			<tagItem v-for="(item, index) in news_cates" :key="index" :itemData="item" @itemClick="handleArticleList" />
		</wCard>
		<baseTitle v-if="projects.length" title="推荐项目" @moreClick="handleProjectList" />
		<view class="project-list">
			<projectItem v-for="(item, index) in projects" :key="index" :itemData="item" />
		</view>
	</view>
</template>

<script setup>
	import {
		navigateTo
	} from '@/utils/util.js'
	import mainInput from './components/mainInput.vue'
	import banner from './components/banner.vue'
	import wCard from '@/components/w-card.vue'
	import tagItem from '@/components/tagItem.vue'
	import gridItem from './components/gridItem.vue'
	import baseTitle from './components/baseTitle.vue'
	import projectItem from '@/components/projectItem.vue'

	import {
		ref
	} from 'vue'
	import {
		login,
		getUserInfo,
		getIndexData
	} from '@/api/index.js'
	import {
		onLoad,
		onShareAppMessage
	} from '@dcloudio/uni-app'
	const news_cates = ref([])
	const projects = ref([])
	const sliders = ref([])
	const project_cates = ref([])

	onLoad(() => {
		uni.login({
			success: (res) => {
				console.log('login', res);
				loginWithCode(res.code)
			}
		})
	})


	const getMainUserInfo = async () => {
		let res = await getUserInfo()
		if (res?.code) {
			console.log("getUserInfo=>", res.data);
			uni.setStorageSync("userInfo", res.data)
		}
	}

	const getMainData = async () => {
		let res = await getIndexData()
		if (res?.code) {
			console.log(res.data);
			news_cates.value = res.data?.news_cates || []
			projects.value = res.data?.projects || []
			sliders.value = res.data?.sliders || []
			project_cates.value = res.data?.project_cates || []
		}
		getMainUserInfo()
	}
	const loginWithCode = async (code) => {
		let res = await login({
			'code': code
		})
		if (res?.code) {
			console.log(res.data);
			console.log(res.data.token.token);
			uni.setStorageSync('token', res.data.token.token)

			getMainData()
		}
	}
	const handleArticleList = (e) => {
		if (e.id) {
			navigateTo({
				url: `/pages/articleList/index?cateids=${e.id}`
			})
		} else {
			navigateTo({
				url: `/pages/articleList/index`
			})
		}
	}

	const handleProjectList = (e) => {
		navigateTo({
			url: `/pages/recommendationItem/index`
		})
	}
	const handleSearch = (e) => {
		navigateTo({
			url: `/pages/search/index`
		})
	}
	const handleMenuClick = (item) => {
		navigateTo({
			url: `/pages/recommendationItem/index?cateids=${item.id}`
		})
	}

	onShareAppMessage((res) => {
		return {
			title: '客满',
			path: '/pages/index/index'
		}
	})
</script>

<style lang="scss" scoped>
	.top-bg {
		width: 100%;
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		z-index: -1;
		display: block;
	}

	.project-list {
		margin-top: 4rpx;
		padding-bottom: 20rpx;
	}
</style>